package com.kinon.tyjaccount.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.kinon.tyjaccount.R;
import com.kinon.tyjaccount.sqlitebean.SettletmentBean;
import com.kinon.tyjaccount.view.GongzuorenyuanActivity;
import com.kinon.tyjaccount.view.KefuActivity;
import com.kinon.tyjaccount.view.LoginActivity;
import com.kinon.tyjaccount.view.MainActivity;
import com.kinon.tyjaccount.view.UrgentSetActivity;

import org.litepal.crud.DataSupport;

/**设置*/

public class ShezhiFragment extends Fragment implements OnClickListener{
	
	private Button btn_gongzuorenyuan;
	private Button btn_lianxikefu;
//	private Button btn_shezhi;
	private Button btn_tuichudenglu;
	private Button btn_urgent;
	
	View iscatview = null;
	private Context mContext;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContext=getActivity();
		return inflater.inflate(R.layout.fragment_shezhi, null);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initRes();
	}
	private void initRes() {
		btn_gongzuorenyuan=(Button)getView().findViewById(R.id.btn_gongzuorenyuan);
		btn_lianxikefu=(Button)getView().findViewById(R.id.btn_lianxikefu);
//		btn_shezhi=(Button)getView().findViewById(R.id.btn_shezhi);
		btn_tuichudenglu=(Button)getView().findViewById(R.id.btn_tuichudenglu);
		btn_urgent= (Button) getView().findViewById(R.id.btn_urgent);

		btn_gongzuorenyuan.setOnClickListener(this);
		btn_lianxikefu.setOnClickListener(this);
		btn_tuichudenglu.setOnClickListener(this);
		btn_urgent.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()){
			case R.id.btn_tuichudenglu:
				DataSupport.deleteAll(SettletmentBean.class);
				tuichudialog();
				break;
			case R.id.btn_gongzuorenyuan:
				Intent intent = new Intent(getActivity(), GongzuorenyuanActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_lianxikefu:
				Intent intentkf = new Intent(getActivity(), KefuActivity.class);
				startActivity(intentkf);
				break;
			case R.id.btn_urgent:
				Intent intentU = new Intent(getActivity(), UrgentSetActivity.class);
				startActivity(intentU);
				break;
		}
	}
	
	/**退出登录弹窗*/
	private void tuichudialog() {

		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		iscatview = LayoutInflater.from(getActivity()).inflate(
				R.layout.dialog_pay, null);
		TextView tital = (TextView) iscatview.findViewById(R.id.tital);
		TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
		TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
		tital.setText("是否退出登录?");
		dialog.setContentView(iscatview);
		dialog.show();
		tv_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		tv_yes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedPreferences spuser = getActivity().getSharedPreferences(
						"spuser", Context.MODE_PRIVATE);
				Editor spusereditor=spuser.edit();
				spusereditor.clear();
				spusereditor.commit();
				
				SharedPreferences sp = getActivity().getSharedPreferences(
						"spReg", Context.MODE_PRIVATE);
				Editor editor=sp.edit();
				editor.clear();
				editor.commit();
				dialog.dismiss();
				Intent logIntent = new Intent(mContext,
						LoginActivity.class);
				startActivity(logIntent);
				MainActivity.context.finish();
//				getActivity().finish();

			}
		});
	}

}
